//
//  MangeCommunityVC.m
//  QRMaster
//
//  Created by myApple on 15/12/7.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "MangeCommunityVC.h"
#import "RequestNetWork.h"
#import "PromptInfoView.h"
#import "TouchBtnColor.h"
#import "OpertionPlist.h"
#import "AppDelegate.h"
#import "checkPower.h"
#import "CommunityInfoLogic.h"
#import "communityModel.h"
#import "setLockPwdViewController.h"
#import "SetLockViewController.h"
#import "DatePickerViewController.h"
#import "ManagerShareCardController.h"
#import "QRSingleton.h"
#import "CommunityInfoModel.h"
#import "MJExtension.h"
#import "BackTableViewCell.h"
#import "selectCommunityController.h"
#import "CommunityListModel.h"
#import "AuthenticateCommunityViewController.h"

#define fontSize [UIFont systemFontOfSize:14.0]
#define ViewHeigth  50

@interface MangeCommunityVC ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
{
    UILabel * titleLable;
    UILabel * communityLabel;
    UILabel * groupNameLabel;
    UILabel * addressLabel;
    UILabel * typeLabel;
    UILabel * userNameLabel;
    UILabel * telePhoneLable;
    UILabel * timeStatusLabel;
    NSString * nowCommunity;
    UILabel * powerNameLable;
    UIView * setBlockView;
    UIView * powerBlockView;
    UIActivityIndicatorView * _activityIndicator;
    QRSingleton * singleton;
    NSArray * powerArray;
    CommunityInfoLogic * comDataBase;//集群基本
    UIView  * titleView ;//不是删除状态情况下的导航栏
    UIButton * addBtn;
    AppDelegate * comApp;
    
    UITableView * backTabelView;
    NSArray * backChooseArray;
    NSArray * backChooseImgArray;
    UIImageView * allBackImgView;//点击加号是遮住整个view的透明的遮罩层
    CGRect backTabelViewFrame;
    UIImageView * authenImageView;//认证图标
    UILabel * authenLabel;//认证状态
    BOOL isCommunityRoleFlag;//是否是超级管理
    //
    UILabel * address;
    UILabel * type;
    UILabel * userName;
    UILabel * telePhone;
    UILabel * powerName;
    UILabel * setLock;
    UILabel * setLockPassWord;
    UILabel * setTime;
    UILabel * shareCard;
    
    
    NSString * addressStr;
    NSString * typeStr;
    NSString * userNameStr;
    NSString * telePhoneStr;
    NSString * powerNameStr;
    NSString * setLockStr;
    NSString * setTimeStr;
    NSString * shareCardStr;
}

@end

@implementation MangeCommunityVC

-(id)init
{
    self = [super init];
    if (self) {
        singleton = [QRSingleton sharedQRSingleton];

    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareMainView];
    comApp = [UIApplication sharedApplication].delegate;
    self.view.backgroundColor = [UIColor whiteColor];
     self.navigationItem.title = NSLocalizedString(@"managerMent", nil);
    backChooseArray = @[NSLocalizedString(@"changCommunity", nil),NSLocalizedString(@"showCommunity", nil),NSLocalizedString(@"refreshCommunity", nil)];
    backChooseImgArray = @[@"ic_change20",@"ic_eye20",@"ic_refreshCommunity"];
    powerArray = @[NSLocalizedString(@"manager", nil),NSLocalizedString(@"groupManagement", nil),NSLocalizedString(@"general", nil),NSLocalizedString(@"nil", nil),NSLocalizedString(@"groupManager", nil)];
//    [self prepareBtnView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLanguage) name:@"changeLanguage"object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeHeight:) name:ChangeHeight object:nil];//改变tableview的高度
   [self prepareBtnView];
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (singleton.managementIndex == 2) {
        [addBtn removeFromSuperview];
        [titleLable removeFromSuperview];
        [self prepareTitleView];
       timeStatusLabel.text = singleton.timeStatus == YES?NSLocalizedString(@"communityTimeOpen", nil):NSLocalizedString(@"communityTimeClose", nil);
    }
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    addBtn.hidden = YES;
    titleLable.hidden = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


-(void)showVc:(NSInteger)index
{
    if (index == 2) {
        [self viewWillAppear:YES];
        if (![nowCommunity isEqualToString:singleton.communityId]) {
            nowCommunity = singleton.communityId;
            [self getDataFromDisk];
            [self getMySelfPower:nowCommunity];
            [self getCommunityInfo:nowCommunity];
        }
         nowCommunity = singleton.communityId;
    }
}

-(void)hiddenVc:(NSInteger)index
{
    if (index == 2) {
        [self viewWillDisappear:YES];
    }
}

-(void)prepareTitleView
{
    addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(UI_SCREEN_WIDTH-40, 2, 40, 40);
    [addBtn addTarget:self action:@selector(hiddenOrShowBackTableView) forControlEvents:UIControlEventTouchUpInside];
    [addBtn setImage: [UIImage imageNamed:@"ic_add30"] forState:0];
    [self.navigationController.navigationBar addSubview:addBtn];
    
    titleLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, UI_SCREEN_WIDTH, 20)];
    titleLable.text =  NSLocalizedString(@"managerMent", nil);
    titleLable.textAlignment = NSTextAlignmentCenter;
    titleLable.font = [UIFont boldSystemFontOfSize:18.0];
    titleLable.textColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:titleLable];
    
    
}

#pragma mark -- 隐藏＋号或者显示
-(void)hiddenOrShowBackTableView
{
    CGRect replaceRect = backTabelViewFrame;
    if (!allBackImgView.hidden) {
        replaceRect.origin.x = replaceRect.origin.x + 80;
        replaceRect.size.width = replaceRect.size.width / 4.0;
        replaceRect.size.height = 0;
        [UIView animateWithDuration:0.1 animations:^{
            backTabelView.frame = replaceRect;
            backTabelView.alpha = 0;
        } completion:^(BOOL finished) {
            backTabelView.frame = backTabelViewFrame;
            allBackImgView.hidden = !allBackImgView.hidden;
        }];
    }
    else
    {
        allBackImgView.hidden = !allBackImgView.hidden;
        backTabelView.alpha = 1;
    }
    
}

#pragma mark -- 点击加号
-(void)prepareBtnView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight;
    allBackImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    allBackImgView.userInteractionEnabled = YES;
    allBackImgView.hidden = YES;
    comApp.window = [UIApplication sharedApplication].keyWindow;
    [comApp.window addSubview:allBackImgView];
    UITapGestureRecognizer * tapAllBackView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenOrShowBackTableView)];
    tapAllBackView.delegate = self;
    [allBackImgView addGestureRecognizer:tapAllBackView];
    
    x = allBackImgView.frame.size.width - 34;
    y = 66;
    width = 11;
    height = 8;
    UIImageView * triangleView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    triangleView.image = [UIImage imageNamed:@"triangle"];
    triangleView.backgroundColor = [UIColor clearColor];
    [allBackImgView addSubview:triangleView];
    
    x = ScreenWidth - 144;
    y = 70;
    width = 138;
    height = backChooseArray.count * 40;
    backTabelView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStylePlain];
    backTabelView.dataSource = self;
    backTabelView.delegate = self;
    backTabelView.bounces = NO;
    backTabelView.layer.cornerRadius = 2;
    backTabelView.layer.masksToBounds = YES;
    backTabelView.userInteractionEnabled = YES;
    backTabelView.separatorStyle = NO;
    [allBackImgView addSubview:backTabelView];
    backTabelViewFrame = backTabelView.frame;
}

#pragma mark -- 解决tableview中手势冲突
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    // 若为UITableViewCellContentView（即点击了tableViewCell），则不截获Touch事件
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return  YES;
}

-(void)queryCommunityInfo:(NSString *)currentCommunityId
{
    comDataBase = [[CommunityInfoLogic alloc]init];
    [comDataBase queryCommunityInfo:currentCommunityId communityInfoModel:^(CommunityInfoModel *communityInfoModel) {
        communityLabel.text = communityInfoModel.name;
        addressLabel.text = communityInfoModel.address;
        typeLabel.text = communityInfoModel.type;
        userNameLabel.text = communityInfoModel.contact;
        telePhoneLable.text = communityInfoModel.telephone;
        timeStatusLabel.text = [communityInfoModel.timeStatus integerValue] == 1? NSLocalizedString(@"communityTimeOpen", nil):NSLocalizedString(@"communityTimeClose", nil);
    }];
}

//获取本地数据
-(void)getDataFromDisk
{
    [self queryCommunityInfo:[[NSUserDefaults standardUserDefaults] objectForKey:@"communityId"]];
}


-(void)changeLanguage
{
//    [self refreshCommunityInfo];
    address.text = NSLocalizedString(@"address", nil);
    type.text = NSLocalizedString(@"style", nil);
    userName.text = NSLocalizedString(@"contacts", nil);
    telePhone.text = NSLocalizedString(@"telePhone", nil);
    powerName.text = NSLocalizedString(@"myPower", nil);
    setLock.text = NSLocalizedString(@"lockSetting", nil);
    setLockPassWord.text = NSLocalizedString(@"lockPwdSetting", nil);
    setTime.text = NSLocalizedString(@"communityTime", nil);
    shareCard.text = NSLocalizedString(@"communityShareList", nil);
    powerArray = @[NSLocalizedString(@"manager", nil),NSLocalizedString(@"groupManagement", nil),NSLocalizedString(@"general", nil),NSLocalizedString(@"nil", nil),NSLocalizedString(@"groupManager", nil)];
    backChooseArray = @[NSLocalizedString(@"changCommunity", nil),NSLocalizedString(@"showCommunity", nil),NSLocalizedString(@"refreshCommunity", nil)];
    [backTabelView reloadData];
}

-(void)prepareMainView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = self.view.frame.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight / 4.0;
    
    _scrollerViewNew = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, UI_SCREEN_HEIGHT - 49 - 144)];
    _scrollerViewNew.showsVerticalScrollIndicator = NO;
    _scrollerViewNew.backgroundColor = RGBA(237, 238, 241, 1);
    [self.view addSubview:_scrollerViewNew];
    
    //物业背景图片
    UIImageView * titleBackImgView =[[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    titleBackImgView.image = [UIImage imageNamed:@"物业背景"];
    titleBackImgView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(authenticateClick)];
    [titleBackImgView addGestureRecognizer:tap];
    [_scrollerViewNew addSubview:titleBackImgView];
    
    
    x = ScreenWidth / 2.0 - 25;
    y = height / 2.0 - 40;
    width = 50;
    height = 50;
    UIImageView * communityImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    communityImgView.image = [UIImage imageNamed:@"ic_house"];
    [titleBackImgView addSubview:communityImgView];
    
    x = 0;
    y += height;
    height = 20;
    width = ScreenWidth;
    communityLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    communityLabel.font = [UIFont boldSystemFontOfSize:18.0];
    communityLabel.textAlignment = NSTextAlignmentCenter;
    communityLabel.textColor = [UIColor whiteColor];
    [titleBackImgView addSubview:communityLabel];
    
    x = 0;
    y += height;
    height = 20;
    width = ScreenWidth;
    groupNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    groupNameLabel.textAlignment = NSTextAlignmentCenter;
    groupNameLabel.textColor = [UIColor whiteColor];
    groupNameLabel.font = [UIFont systemFontOfSize:16.0];
    [titleBackImgView addSubview:groupNameLabel];
    
    //认证状态
    x = ScreenWidth / 2.0 - 15;
//    y += height;
    y += 5;
    width = 12.5;
    height = 15;
    authenImageView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    authenImageView.image = [UIImage imageNamed:@"authenImage"];
    [titleBackImgView addSubview:authenImageView];
    authenImageView.hidden = YES;
    
    x += width + 5;
    width = ScreenWidth - x;
    height = 15;
    authenLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    authenLabel.font = [UIFont systemFontOfSize:15];
    authenLabel.textColor = [UIColor whiteColor];
    [titleBackImgView addSubview:authenLabel];
    
    UIFont * font = [UIFont systemFontOfSize:14.0];
    float offsetY = 15;
    float labelHeight = 20;
    float beginX = 20;
    
    //基本属性块
    x = 0;
    y = titleBackImgView.frame.origin.y + titleBackImgView.frame.size.height;
    width = ScreenWidth;
    height = 210;
    UIView * basicBlockView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    basicBlockView.backgroundColor = RGBA(237, 238, 241, 1);
    [_scrollerViewNew addSubview:basicBlockView];
    
    //地址
    x = 0;
    y = 5;
    width = ScreenWidth;
    height = ViewHeigth;
    UIView * AddressView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    AddressView.backgroundColor = RGBA(248, 248, 248, 1);
    [basicBlockView addSubview:AddressView];
    
    x = beginX;
    y = offsetY;
    width = 60;
    height = labelHeight;
    address = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    address.text = NSLocalizedString(@"address", nil);
    address.font = font;
    [AddressView addSubview:address];
    
    x = ScreenWidth / 2.0 - 40;
    y = 5;
    height = 40;
    width = AddressView.frame.size.width - x - 20;
    addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    addressLabel.font = font;
    addressLabel.numberOfLines = 2;
    addressLabel.textColor = RGBA(0, 0, 0, 0.8);
    addressLabel.textAlignment = NSTextAlignmentRight;
    [AddressView addSubview:addressLabel];
    
    //类型
    x = 0;
    y = AddressView.frame.origin.y + AddressView.frame.size.height + 1;
    width = ScreenWidth;
    height = ViewHeigth;
    UIView * typeView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeView.backgroundColor = RGBA(248, 248, 248, 1);
    [basicBlockView addSubview:typeView];
    
    x = beginX;
    width = 60;
    height = labelHeight;
    y = offsetY;
    type = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    type.font = font;
    type.text = NSLocalizedString(@"style", nil);
    [typeView addSubview:type];
    
    x = ScreenWidth / 2.0 - 40;
    width = typeView.frame.size.width - x - 20;
    typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    typeLabel.font = font;
    typeLabel.textColor = RGBA(0, 0, 0, 0.8);
    typeLabel.textAlignment = NSTextAlignmentRight;
    [typeView addSubview:typeLabel];
    
    //联系人
    x = 0;
    y = typeView.frame.origin.y + typeView.frame.size.height + 1;
    width = ScreenWidth;
    height = ViewHeigth;
    UIView * userNameView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    userNameView.backgroundColor = RGBA(248, 248, 248, 1);
    [basicBlockView addSubview:userNameView];
    
    x = beginX;
    y = offsetY;
    width = 100;
    height = labelHeight;
    userName = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    userName.font = font;
    userName.text = NSLocalizedString(@"contacts", nil);
    [userNameView addSubview:userName];
    
    x = ScreenWidth / 2.0 - 40;
    width = userNameView.frame.size.width - x - 20;
    userNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    userNameLabel.font = font;
    userNameLabel.textColor = RGBA(0, 0, 0, 0.8);
    userNameLabel.textAlignment = NSTextAlignmentRight;
    [userNameView addSubview:userNameLabel];
    
    //手机
    x = 0;
    y = userNameView.frame.origin.y + userNameView.frame.size.height + 1;
    width = ScreenWidth;
    height = ViewHeigth;
    UIView * telePhoneView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneView.backgroundColor = RGBA(248, 248, 248, 1);
    [basicBlockView addSubview:telePhoneView];
    
    x = beginX;
    y = offsetY;
    width = 100;
    height = labelHeight;
    telePhone = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhone.font = font;
    telePhone.text = NSLocalizedString(@"telePhone", nil);
    [telePhoneView addSubview:telePhone];
    
    x = ScreenWidth / 2.0 - 40;
    width = telePhoneView.frame.size.width - x - 20;
    telePhoneLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    telePhoneLable.font = font;
    telePhoneLable.textColor = RGBA(0, 0, 0, 0.8);
    telePhoneLable.textAlignment = NSTextAlignmentRight;
    [telePhoneView addSubview:telePhoneLable];
    
    
    //权限块
    x = 0;
    y = basicBlockView.frame.size.height + basicBlockView.frame.origin.y + 10;
    width = ScreenWidth;
    height = 50;
    powerBlockView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    powerBlockView.backgroundColor = RGBA(237, 238, 241, 1);
    [_scrollerViewNew addSubview:powerBlockView];
    
    //拥有权限
    x = 0;
    y = 0;
    width = ScreenWidth;
    height = ViewHeigth;
    UIButton * myPowerBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [myPowerBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:myPowerBtn.frame.size] forState:UIControlStateHighlighted];
    myPowerBtn.backgroundColor = RGBA(248, 248, 248, 1);
    [powerBlockView addSubview:myPowerBtn];
    
    x = beginX;
    y = offsetY;
    width = 120;
    height = labelHeight;
    powerName = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    powerName.font = font;
    powerName.text = NSLocalizedString(@"myPower", nil);
    [myPowerBtn addSubview:powerName];
    
    x = ScreenWidth / 2.0 - 30;
    y = 5;
    width = myPowerBtn.frame.size.width - x - 20;
    height = 40;
    powerNameLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    powerNameLable.font = font;
    powerNameLable.numberOfLines = 2;
    powerNameLable.textColor = RGBA(0, 0, 0, 0.8);
    powerNameLable.textAlignment = NSTextAlignmentRight;
    [myPowerBtn addSubview:powerNameLable];
    
    
    //设置块
    x = 0;
    y = powerBlockView.frame.size.height + powerBlockView.frame.origin.y + 10;
    width = ScreenWidth;
    height = 200;
    setBlockView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setBlockView.backgroundColor = RGBA(237, 238, 241, 1);
    [_scrollerViewNew addSubview:setBlockView];
    
    //门锁设置
    x = 0;
    y = 0;
    width = ScreenWidth;
    height = ViewHeigth;
    UIButton * setLockBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [setLockBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:setLockBtn.frame.size] forState:UIControlStateHighlighted];
    [setLockBtn addTarget:self action:@selector(setLock) forControlEvents:UIControlEventTouchUpInside];
    [setLockBtn setBackgroundColor:RGBA(248, 248, 248, 1)];
    [setBlockView addSubview:setLockBtn];
    
    x = beginX;
    y = offsetY;
    width = 80;
    height = labelHeight;
    setLock = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setLock.font = font;
    setLock.text = NSLocalizedString(@"lockSetting", nil);
    [setLockBtn addSubview:setLock];
    
    x = ScreenWidth / 2.0 - 40;
    width = setLockBtn.frame.size.width - x;
    UILabel * setLockLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setLockLable.font = font;
    setLockLable.textColor = RGBA(0, 0, 0, 0.8);
    [setLockBtn addSubview:setLockLable];
    
    x = setLockBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * setLockImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setLockImgView.image = [UIImage imageNamed:@"36右箭头"];
    [setLockBtn addSubview:setLockImgView];
    
    //密钥设置
    x = 0;
    y = setLockBtn.frame.origin.y + setLockBtn.frame.size.height + 1;
    width = ScreenWidth;
    height = ViewHeigth;
    UIButton * setLockPwdBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [setLockPwdBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:setLockBtn.frame.size] forState:UIControlStateHighlighted];
    [setLockPwdBtn addTarget:self action:@selector(setLockPwd) forControlEvents:UIControlEventTouchUpInside];
    [setLockPwdBtn setBackgroundColor:RGBA(248, 248, 248, 1)];
    [setBlockView addSubview:setLockPwdBtn];
    
    x = beginX;
    y = offsetY;
    width = 120;
    height = labelHeight;
    setLockPassWord = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setLockPassWord.font = font;
    setLockPassWord.text = NSLocalizedString(@"lockPwdSetting", nil);
    [setLockPwdBtn addSubview:setLockPassWord];
    
    x = ScreenWidth / 2.0 - 40;
    width = setLockPwdBtn.frame.size.width - x;
    UILabel * setLockPwdLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setLockPwdLable.font = font;
    setLockPwdLable.textColor =RGBA(0, 0, 0, 0.8);
    [setLockPwdBtn addSubview:setLockPwdLable];
    
    x = setLockPwdBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * setLockPwdImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setLockPwdImgView.image = [UIImage imageNamed:@"36右箭头"];
    [setLockPwdBtn addSubview:setLockPwdImgView];
    
    //时间属性
    x = 0;
    y = setLockPwdBtn.frame.origin.y + setLockPwdBtn.frame.size.height + 1;
    width = ScreenWidth;
    height = ViewHeigth;
    UIButton * setTimeBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [setTimeBtn setBackgroundColor:RGBA(248, 248, 248, 1)];
    [setTimeBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:setTimeBtn.frame.size] forState:UIControlStateHighlighted];
    [setTimeBtn addTarget:self action:@selector(setTime) forControlEvents:UIControlEventTouchUpInside];
    [setBlockView addSubview:setTimeBtn];
    
    x = beginX;
    y = offsetY;
    width = 120;
    height = labelHeight;
    setTime = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setTime.font = font;
    setTime.text = NSLocalizedString(@"communityTime", nil);
    [setTimeBtn addSubview:setTime];
    
    x = ScreenWidth / 2.0 - 40;
    width = setTimeBtn.frame.size.width - x;
    UILabel * setTimeLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setTimeLable.font = font;
    setTimeLable.textColor = RGBA(0, 0, 0, 0.8);
    [setTimeBtn addSubview:setTimeLable];
    
    x = setTimeBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * setTimeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    setTimeImgView.image = [UIImage imageNamed:@"36右箭头"];
    [setTimeBtn addSubview:setTimeImgView];
    
    x = setTimeBtn.frame.size.width - 100;
    width = 60;
    y = 15;
    height = 20;
    timeStatusLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    timeStatusLabel.font = [UIFont systemFontOfSize:12.0];
    timeStatusLabel.textColor = [UIColor grayColor];
    [setTimeBtn addSubview:timeStatusLabel];
    
    //分享操作
    x = 0;
    y = setTimeBtn.frame.origin.y + setTimeBtn.frame.size.height + 1;
    width = ScreenWidth;
    height = ViewHeigth;
    UIButton * shareCardBtn = [[UIButton alloc]initWithFrame:CGRectMake(x, y, width, height)];
    [shareCardBtn setBackgroundImage:[TouchBtnColor imageWithColor:RGBA(237, 238, 241, 1) size:shareCardBtn.frame.size] forState:UIControlStateHighlighted];
    [shareCardBtn addTarget:self action:@selector(shareCard) forControlEvents:UIControlEventTouchUpInside];
    [shareCardBtn setBackgroundColor:RGBA(248, 248, 248, 1)];
    [setBlockView addSubview:shareCardBtn];
    
    x = beginX;
    y = offsetY;
    width = 100;
    height = labelHeight;
    shareCard = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    shareCard.font = font;
    shareCard.text = NSLocalizedString(@"communityShareList", nil);
    [shareCardBtn addSubview:shareCard];
    
    x = ScreenWidth / 2.0 - 40;
    width = setLockBtn.frame.size.width - x;
    UILabel * shareCardkLable = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    shareCardkLable.font = font;
    shareCardkLable.textColor = RGBA(0, 0, 0, 0.8);
    [shareCardBtn addSubview:shareCardkLable];
    
    x = setLockBtn.frame.size.width - 40;
    y = 15;
    height = 20;
    width = 20;
    UIImageView * shareCardkImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    shareCardkImgView.image = [UIImage imageNamed:@"36右箭头"];
    [shareCardBtn addSubview:shareCardkImgView];
    
    _scrollerViewNew.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, powerBlockView.frame.size.height + powerBlockView.frame.origin.y + 10);
    setBlockView.hidden = YES;
    [self queryCommunityInfo:[[NSUserDefaults standardUserDefaults] objectForKey:@"communityId"]];
}

-(void)authenticateClick
{
    if (!isCommunityRoleFlag) {
        [PromptInfoView showMessage:NSLocalizedString(@"noPowerModifyAuthen", @"你没有修改认证信息的权限") superView:self.view];
        return;
    }
    
    AuthenticateCommunityViewController * acvc = [[AuthenticateCommunityViewController alloc]init];
    acvc.hidesBottomBarWhenPushed = YES;
    acvc.communityStyle = typeLabel.text;
    acvc.status = ^(NSInteger status)
    {
        authenImageView.hidden = YES;
        switch (status) {
            case 0:
                authenLabel.text = NSLocalizedString(@"notAuthen", @"未认证");
                break;
            case 1:
                authenLabel.text = NSLocalizedString(@"needVerify", @"认证中");
                break;
            case 2:
                authenLabel.text = NSLocalizedString(@"authenicate", @"已认证");
                authenImageView.hidden = NO;
                break;
            case 3:
                authenLabel.text = NSLocalizedString(@"authenicate", @"已认证");
                authenImageView.hidden = NO;
                break;
            default:
                authenLabel.text = NSLocalizedString(@"authenfalse", @"认证失败");
                break;
        }
    };
    [self.navigationController pushViewController:acvc animated:YES];
}

#pragma mark --- 删除cell

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return backChooseArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString * cellString = @"cell";
        BackTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellString];
        if (!cell)
        {
            cell = [[BackTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellString];
        }
        cell.selectedBackgroundView = [[UIImageView alloc]initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = [UIColor blackColor];
        cell.contentView.backgroundColor = [UIColor blackColor];
        cell.imgString = backChooseImgArray[indexPath.row];
        cell.content = backChooseArray[indexPath.row];
        return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 0) {
            [self changeCommunity];
        }
        else if(indexPath.row == 1)
        {
            [[NSNotificationCenter defaultCenter]postNotificationName:@"showCommunityName" object:nil];
        }
        else
        {
            [self refreshCommunityInfo];
        }
        [self hiddenOrShowBackTableView];
        [backTabelView deselectRowAtIndexPath:[backTabelView indexPathForSelectedRow] animated:YES];
}


#pragma mark -- 切换集群
-(void)changeCommunity
{
    selectCommunityController * scc = [[selectCommunityController alloc]init];
    scc.communityId = singleton.communityId;
    [self.navigationController pushViewController:scc animated:YES];
    scc.communityBlock = ^(CommunityListModel * model)
    {
        nowCommunity = model.communityId;
        [self getMySelfPower:nowCommunity];
        [self getCommunityInfo:nowCommunity];
    };
}

-(void)changeHeight:(NSNotification *)not
{
    CGFloat height = [[not.userInfo objectForKey:@"height"]floatValue];
    _scrollerViewNew.frame = CGRectMake(0, 0, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 64 - height - 49);
}

-(void)setLock
{
    SetLockViewController * sckc = [[SetLockViewController alloc]init];
    sckc.hidesBottomBarWhenPushed = YES;
    sckc.communityId = nowCommunity;
    [self.navigationController pushViewController:sckc animated:YES];
}

-(void)setLockPwd
{
    setLockPwdViewController * slpv = [[setLockPwdViewController alloc]init];
    slpv.communityId = nowCommunity;
    slpv.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:slpv animated:YES];
}

-(void)setTime
{
    DatePickerViewController * spvc = [[DatePickerViewController alloc]init];
    spvc.communityId = nowCommunity;
    spvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:spvc animated:YES];
}

-(void)refreshCommunityInfo
{
    [self getMySelfPower:nowCommunity];
    [self getCommunityInfo:nowCommunity];
}
-(void)getMySelfPower:(NSString *)communityId
{
    _scrollerViewNew.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, powerBlockView.frame.size.height + powerBlockView.frame.origin.y + 10);
    setBlockView.hidden = YES;

    if ([self IsHaveNetwork]) {
        return;
    }
    checkPower * power = [[checkPower alloc]init];
    [power getUserPower];
    power.powerBlock = ^(id data,id error)
    {
        if ([data objectForKey:@"data"])
        {
            powerNameLable.text = NSLocalizedString(@"no", nil);
            NSDictionary * communtiyDict = [data objectForKey:@"data"];
            NSArray * CommunityArray = [communtiyDict objectForKey:@"communities"];
            for (NSDictionary * dict in CommunityArray) {
                if ([[dict objectForKey:@"communityid"]isEqualToString:communityId]) {
                    powerNameLable.text = powerArray[[[dict objectForKey:@"role"]integerValue] - 1];
                    if ([[dict objectForKey:@"role"]isEqualToString:@"1"]) {
                        _scrollerViewNew.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, setBlockView.frame.size.height + setBlockView.frame.origin.y + 10);
                        setBlockView.hidden = NO;
                        isCommunityRoleFlag = YES;
                    }
                    else
                    {
                        isCommunityRoleFlag = NO;
                    }
                }
            }
            
        }
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"] superView:self.view];
        }
    };
    
}


-(void)getCommunityInfo:(NSString *)communityid
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:communityid forKey:@"communityId"];
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(NEWCOMMUNITYINFO) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        NSDictionary * data = [downLoadData objectForKey:@"data"];
        if (data) {
            communityLabel.text = [data objectForKey:@"name"];
            addressLabel.text = [data objectForKey:@"address"];
            typeLabel.text = [data objectForKey:@"type"];
            userNameLabel.text = [data objectForKey:@"contact"];
            authenImageView.hidden = YES;
            switch ([[data objectForKey:@"authStatus"]integerValue]) {
                case 0:
                    authenLabel.text = NSLocalizedString(@"notAuthen", @"未认证");
                    break;
                case 1:
                    authenLabel.text = NSLocalizedString(@"needVerify", @"认证中");
                    break;
                case 2:
                    authenLabel.text = NSLocalizedString(@"authenicate", @"已认证");
                    authenImageView.hidden = NO;
                    break;
                case 3:
                    authenLabel.text = NSLocalizedString(@"authenicate", @"已认证");
                    authenImageView.hidden = NO;
                    break;
                default:
                    authenLabel.text = NSLocalizedString(@"authenfalse", @"认证失败");
                    break;
            }
            
            NSString * areaCode = [data objectForKey:@"areaCode"];
            if (![areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString] && ![areaCode isEqualToString:@""]) {
                telePhoneLable.text = [NSString stringWithFormat:@"+%@ %@",areaCode,[data objectForKey:@"telephone"]];
            }
            else
            {
                telePhoneLable.text = [data objectForKey:@"telephone"];
            }
            groupNameLabel.text = [data objectForKey:@"groupname"];
            timeStatusLabel.text = [[data objectForKey:@"timeStatus"]integerValue] == 1?NSLocalizedString(@"communityTimeOpen", nil):NSLocalizedString(@"communityTimeClose", nil);
            singleton.timeStatus = [[data objectForKey:@"timeStatus"]integerValue] == 1?YES:NO;
            CommunityInfoModel * model = [CommunityInfoModel mj_objectWithKeyValues:data];
            comDataBase = [[CommunityInfoLogic alloc]init];
            [comDataBase deleteCommunityInfo:communityid];
            [comDataBase insertCommunityData:model withCommuntiyId:communityid member_timestamp:nil customer_timestamp:nil group_timestamp:nil room_timestamp:nil];
//            [self getDataFromDisk];
        }
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
        
    }];
}

-(void)shareCard
{
    ManagerShareCardController * mscc = [[ManagerShareCardController alloc]init];
     mscc.communityId = nowCommunity;
    mscc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:mscc animated:YES];
}

#pragma mark -- 当职员或者客户改变集群时，物业信息也要改变
-(void)changeCommunity:(NSNotification *)not
{
    nowCommunity = [not.userInfo objectForKey:@"communityid"];
//    [self getMySelfPower:nowCommunity];
//    [self getCommunityInfo:nowCommunity];
    [self queryCommunityInfo:[[NSUserDefaults standardUserDefaults] objectForKey:@"communityId"]];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


@end
